﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using Ivy.UserRegistration.DataAccess;
using Ivy.UserRegistration.Entity;
using Ivy.Common.Framework.GeneralUtility;
using Ivy.Common.Framework.EmailUtility;
using Ivy.UserRegistration.IBusinessLogic;


namespace Ivy.UserRegistration.BusinessLogic
{
    public class UserRegistrationBus : IUserRegistrationBus
    {
        #region Variables & Constants
        #endregion Variables & Constants

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Public Properties
        #endregion Public Properties

        #region Public Methods
        #region IUserRegistrationBus Members
        #region UserRegistration and profile update
        public bool RegisterNewUser(User UserEntity)
        {
            bool IsOperationSuccessful = false;

            //First check if the user login ID is already present or not. If not preset then insert.
            if (!IsLoginIDExisting(UserEntity.UserLoginID))
            {
                UserDao lUserDao = new UserDao();
                IsOperationSuccessful = lUserDao.Insert(UserEntity);

            }

            return IsOperationSuccessful;
        }

        public bool SendVerificationCodeEmail(User UserEntity)
        {
            bool IsOperationSuccessful = false;

            EmailUtility lEmailUtility = new EmailUtility();
            MailMessage lMailMsg = new MailMessage();
            lMailMsg.Subject = "Dasideen - Registration verification & confirmation mail";
            lMailMsg.IsBodyHtml = true;
            lMailMsg.Body = @"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">";
            lMailMsg.Body = lMailMsg.Body + @"<head> <style type=""text/css""> .style1 { color: #006600; } .style2 { text-align: left; font-size: small; } .style3 { font-size: large; } .style4 { font-size: large; font-weight: bold; } </style> </head>";
            lMailMsg.Body = lMailMsg.Body + @"<body style=""font-family: 'MS Sans Serif'; text-align: center"">";
            lMailMsg.Body = lMailMsg.Body + @"<table class=""style3"">";
            lMailMsg.Body = lMailMsg.Body + @"<tr><td><h1 class=""style1""> Congratulation ! </h1></td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr><td class=""style3""> You have Successfully Registered with Dasideen. </td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr><td class=""style3""> Please Note Following : </td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr><td><span class=""style3"">User Name : </span><span class=""style4"">" + UserEntity.UserFirstName + " " + UserEntity.UserLastName + @"</span><span class=""style3""> </span></td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr><td><span class=""style3"">User Login Id : </span><span class=""style4"">" + UserEntity.UserLoginID + @"</span><span class=""style3""> </span></td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> <span class=""style3"">User Verification Code : </span><span class=""style4"">" + UserEntity.UserVerificationCode + @"</span><span class=""style3""> </span> </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> To activate your membership, please login and confirm your verification code </td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> &nbsp; </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Thanks and Regards </td> </tr> <tr> <td class=""style2""> Dasideen(The Ideas) Administrator </td> </tr> <tr> <td class=""style2""> (A Crowd Community) </td> </tr> <tr> <td class=""style2""> &nbsp; </td> </tr>";
            //lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Follow our project at CodePlex <a href=""http://ivyframeworks.codeplex.com/""> www.ivyframeworks.codeplex.com/</a> </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Copyright &copy; 2012&nbsp; <a href=""http://www.dasideen.com/"">www.dasideen.com</a> </td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"</table></body></html>";

            //lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg, "frameworkivy@gmail.com", "Administrator FrameworkIvy");
            //lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendTo, UserEntity.UserPrimaryEmailID);
            //lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendBcc, "frameworkivy@googlegroups.com");

            lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg);
            lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendTo, UserEntity.UserPrimaryEmailID);
            lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendBcc, lEmailUtility.GetComaSeperatedDefaultAlertMailAddresses());
            //lEmailUtility.AddEmailRecepientForAdminGroupToMailMessage(lMailMsg);

            lEmailUtility.SendEmail(lMailMsg);
            IsOperationSuccessful = true;

            return IsOperationSuccessful;
        }

        public bool SendVerificationCodeForForgetPasswordEmail(User UserEntity)
        {
            bool IsOperationSuccessful = false;
            EmailUtility lEmailUtility = new EmailUtility();
            MailMessage lMailMsg = new MailMessage();
            lMailMsg.Subject = "Dasideen - Forget password verification mail";
            lMailMsg.IsBodyHtml = true;
            lMailMsg.Body = @"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">";
            lMailMsg.Body = lMailMsg.Body + @"<head> <style type=""text/css""> .style1 { color: #006600; } .style2 { text-align: left; font-size: small; } .style3 { font-size: large; } .style4 { font-size: large; font-weight: bold; } </style> </head>";
            lMailMsg.Body = lMailMsg.Body + @"<body style=""font-family: 'MS Sans Serif'; text-align: center"">";
            lMailMsg.Body = lMailMsg.Body + @"<table class=""style3"">";
            lMailMsg.Body = lMailMsg.Body + @"<tr><td><h1 class=""style1""> To reset your password ! </h1></td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr><td class=""style3"">You have received this mail as part of our Forget Password process</td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr><td class=""style3""> Please check the following details and proceed : </td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr><td><span class=""style3"">User Name : </span><span class=""style4"">" + UserEntity.UserFirstName + " " + UserEntity.UserLastName + @"</span><span class=""style3""> </span></td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr><td><span class=""style3"">User Login Id : </span><span class=""style4"">" + UserEntity.UserLoginID + @"</span><span class=""style3""> </span></td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> <span class=""style3"">Forget Password Verification Code : </span><span class=""style4"">" + UserEntity.UserVerificationCode + @"</span><span class=""style3""> </span> </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> To reset your password, please confirm with the above verification code </td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> &nbsp; </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Thanks and Regards </td> </tr> <tr> <td class=""style2""> Dasideen(The Ideas) Administrator </td> </tr> <tr> <td class=""style2""> (A Crowd Community) </td> </tr> <tr> <td class=""style2""> &nbsp; </td> </tr>";
            //lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Follow our project at CodePlex <a href=""http://ivyframeworks.codeplex.com/""> www.ivyframeworks.codeplex.com/</a> </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Copyright &copy; 2012&nbsp; <a href=""http://www.dasideen.com/"">www.dasideen.com</a> </td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"</table></body></html>";

            //lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg, "frameworkivy@gmail.com", "Administrator FrameworkIvy");
            //lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendTo, UserEntity.UserPrimaryEmailID);
            //lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendBcc, "frameworkivy@googlegroups.com");

            lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg);
            lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendTo, UserEntity.UserPrimaryEmailID);
            lEmailUtility.AddEmailRecepientForAdminGroupToMailMessage(lMailMsg);

            lEmailUtility.SendEmail(lMailMsg);
            IsOperationSuccessful = true;

            return IsOperationSuccessful;
        }
        public bool UpdateRegisteredUser(User UserEntity)
        {
            bool IsOperationSuccessful = false;


            //if (Login(UserEntity.UserLoginID, UserEntity.UserPassword) == true)
            //{
            UserDao lUserDao = new UserDao();
            IsOperationSuccessful = lUserDao.Update(UserEntity);
            //}

            return IsOperationSuccessful;
        }

        public bool SaveUserProfile(UserProfile UserProfileEntity)
        {
            bool IsOperationSuccessful = false;

            UserDao lUserDao = new UserDao();
            if (UserProfileEntity.UserKey > 0)
            {
                User lUser = lUserDao.GetByID(UserProfileEntity.UserKey.ToString());
                if (lUser != null)
                {
                    UserProfileDao _UserProfileDao = new UserProfileDao();
                    UserProfile _UserProfile = _UserProfileDao.GetByID(UserProfileEntity.UserKey.ToString());
                    if (_UserProfile != null)
                    {
                        _UserProfileDao = new UserProfileDao();
                        IsOperationSuccessful = _UserProfileDao.Update(UserProfileEntity);
                    }
                    else
                    {
                        _UserProfileDao = new UserProfileDao();
                        IsOperationSuccessful = _UserProfileDao.Insert(UserProfileEntity);
                    }
                }
            }

            return IsOperationSuccessful;
        }

        public bool IsLoginIDExisting(string UserLoginID)
        {
            bool IsLoginIDExisting = true;

            UserDao lUserDao = new UserDao();
            User lUser;
            lUser = lUserDao.GetByLoginID(UserLoginID);
            if (lUser == null)
            {
                IsLoginIDExisting = false;
            }

            return IsLoginIDExisting;
        }

        public bool IsUserVerified(string UserLoginID)
        {
            bool IsUserVerified = false;

            UserDao lUserDao = new UserDao();
            User lUser;
            lUser = lUserDao.GetByLoginID(UserLoginID);
            if (lUser.UserIsVerified == true)
            {
                IsUserVerified = true;
            }

            return IsUserVerified;
        }

        public User GetUserByLoginID(string UserLoginID)
        {

            UserDao lUserDao = new UserDao();
            User lUser;
            lUser = lUserDao.GetByLoginID(UserLoginID);
            if (lUser == null)
            {
                return null;
            }
            else
            {
                return lUser;
            }

        }

        public bool Login(string UserLoginID, string Password)
        {
            bool IsLoginSuccess = false;

            UserDao lUserDao = new UserDao();
            User lUser;
            lUser = lUserDao.GetByLoginID(UserLoginID);
            if (lUser != null)
            {
                if (lUser.UserLoginID == UserLoginID)
                {
                    if (StringUtility.DecryptString(lUser.UserPassword) == Password)
                    {
                        IsLoginSuccess = true;
                    }
                }
            }

            return IsLoginSuccess;
        }

        public UserProfile GetUserProfileByID(string UserID)
        {

            UserProfileDao lUserProfileDao = new UserProfileDao();
            UserProfile lUserProfile = lUserProfileDao.GetByID(UserID);
            if (lUserProfile == null)
            {
                return null;
            }
            else
            {
                return lUserProfile;
            }

        }

        public User GetUserByPrimaryEmailId(string UserPrimaryEmailId)
        {

            UserDao lUserDao = new UserDao();
            User lUser;
            lUser = lUserDao.GetUserByEmailId(UserPrimaryEmailId);
            if (lUser == null)
            {
                return null;
            }
            else
            {
                return lUser;
            }

        }

        public List<User> GetAllUser()
        {
            List<User> lUserList = new List<User>();

            UserDao lUserDao = new UserDao();
            lUserList = lUserDao.GetAll();

            return lUserList;
        }

        public bool IsUserHavingAdministratorAuthorization(int Userkey)
        {
            bool IsUserAdmin = false;

            UserDao lUserDao = new UserDao();
            User lUser;
            lUser = lUserDao.GetUserWithAdministratorAuthorizationByUserKey(Userkey);
            if (lUser != null)
            {
                if (lUser.UserKey == Userkey)
                {
                    IsUserAdmin = true;
                }
            }
            return IsUserAdmin;
        }

        public bool IsUserHavingContentUpdateAuthorization(int UserKey, int ContentKey, string ActivityTypeCode, string ContentTypeCode)
        {
            bool IsUserAuthorized = false;
            UserDao lUserDao = new UserDao();
            IsUserAuthorized = lUserDao.IsUserHavingContentUpdateAuthorization(UserKey, ContentKey, ActivityTypeCode, ContentTypeCode);
            return IsUserAuthorized;
        }

        public List<UserProfileReportList> GetAllRegisterdUserProfileList()
        {
            List<UserProfileReportList> lUserProfileReportList = new List<UserProfileReportList>();

            UserDao lUserDao = new UserDao();
            lUserProfileReportList = lUserDao.GetAllRegisterdUserProfileList();

            return lUserProfileReportList;
        }

        public UserProfileReportList GetUserProfileDetailsByUserKey(int UserKey)
        {
            UserDao lUserDao = new UserDao();
            UserProfileReportList lUserProfileReportList = lUserDao.GetUserByUserKey(UserKey);
            if (lUserProfileReportList == null)
            {
                return null;
            }
            else
            {
                return lUserProfileReportList;
            }
        }
        #endregion UserRegistration and profile update

        #region User Society and Role management
        public bool AssignSocietyAndRoleToUser(int UserKey, int SocietyKey, int RoleKey)
        {
            throw new NotFiniteNumberException();
        }

        public bool RemoveSocietyAndRoleFromUser(int UserKey, int SocietyKey, int RoleKey)
        {
            throw new NotFiniteNumberException();
        }

        public List<UserRole> GetAllRolesForSelectedUser(int UserKey)
        {
            List<UserRole> lUserRoles = new List<UserRole>();
            UserRoleDao lUserRoleDao = new UserRoleDao();
            lUserRoles = lUserRoleDao.GetByUserKey(UserKey);

            return lUserRoles;
        }

        public Sys_RoleMaster GetSelectedSys_RoleMasterByID(int Rolekey)
        {


            Sys_RoleMasterDao lSys_RoleMasterDao = new Sys_RoleMasterDao();
            Sys_RoleMaster lSys_RoleMaster;
            lSys_RoleMaster = lSys_RoleMasterDao.GetByRoleKey(Rolekey);
            if (lSys_RoleMaster == null)
            {
                return null;
            }
            else
            {
                return lSys_RoleMaster;
            }

        }
        #endregion User Society and Role management

        #region Sys_LoginHistory_Log
        public bool RegisterUserLoginHistory(Sys_LoginHistoryLog Sys_LoginHistoryLogEntity)
        {
            bool IsOperationSuccessful = false;


            Sys_LoginHistoryLogDao lSys_LoginHistoryLogDao = new Sys_LoginHistoryLogDao();
            IsOperationSuccessful = lSys_LoginHistoryLogDao.Insert(Sys_LoginHistoryLogEntity);


            return IsOperationSuccessful;
        }

        public bool UpdateRegisteredUserLoginHistory(Sys_LoginHistoryLog Sys_LoginHistoryLogEntity)
        {
            bool IsOperationSuccessful = false;

            Sys_LoginHistoryLogDao lSys_LoginHistoryLogDao = new Sys_LoginHistoryLogDao();
            IsOperationSuccessful = lSys_LoginHistoryLogDao.Update(Sys_LoginHistoryLogEntity);

            return IsOperationSuccessful;
        }

        public Sys_LoginHistoryLog GetUserLoginHistoryByLoginKey(int LoginKey)
        {

            Sys_LoginHistoryLogDao lSys_LoginHistoryLogDao = new Sys_LoginHistoryLogDao();
            Sys_LoginHistoryLog lSys_LoginHistoryLog = lSys_LoginHistoryLogDao.GetByLoginKey(LoginKey);

            if (lSys_LoginHistoryLog == null)
            {
                return null;
            }
            else
            {
                return lSys_LoginHistoryLog;
            }

        }
        #endregion

        #region UserProfileImage
        public bool SaveUserProfileImage(UserProfileImage UserProfileImageEntity)
        {
            bool IsOperationSuccessful = false;
            if (UserProfileImageEntity.UserKey > 0)
            {
                if (UserProfileImageEntity.UserImageData != null)
                {
                    UserProfileImageDao lUserProfileImageDao = new UserProfileImageDao();
                    UserProfileImage lUserProfileImage = lUserProfileImageDao.GetByUserKey(UserProfileImageEntity.UserKey);
                    if (lUserProfileImage != null)
                    {
                        //Update existing record
                        IsOperationSuccessful = lUserProfileImageDao.Update(UserProfileImageEntity);
                    }
                    else
                    {
                        //Insert new record
                        IsOperationSuccessful = lUserProfileImageDao.Insert(UserProfileImageEntity);
                    }
                }
            }
            return IsOperationSuccessful;
        }
        //public bool RegisterNewUserProfileImage(UserProfileImage UserProfileImageEntity)
        //{
        //    bool IsOperationSuccessful = false;

        //    UserProfileImageDao lUserProfileImageDao = new UserProfileImageDao();
        //    IsOperationSuccessful = lUserProfileImageDao.Insert(UserProfileImageEntity);

        //    return IsOperationSuccessful;
        //}

        //public bool UpdateRegisteredUserProfileImage(UserProfileImage UserProfileImageEntity)
        //{
        //    bool IsOperationSuccessful = false;

        //    UserProfileImageDao lUserProfileImageDao = new UserProfileImageDao();
        //    IsOperationSuccessful = lUserProfileImageDao.Update(UserProfileImageEntity);

        //    return IsOperationSuccessful;
        //}

        public UserProfileImage GetUserProfileImageByUserKey(int Userkey)
        {
            UserProfileImageDao lUserProfileImageDao = new UserProfileImageDao();
            UserProfileImage lUserProfileImage = lUserProfileImageDao.GetByUserKey(Userkey);

            if (lUserProfileImage == null)
            {
                return null;
            }
            else
            {
                return lUserProfileImage;
            }
        }
        #endregion

        #region Sys_SessionLog
        public bool SaveUserSys_SessionLogDetails(Sys_SessionLog lSys_SessionLogEntity)
        {
            bool IsOperationSuccessful = false;


            Sys_SessionLogDao lSys_SessionLogDao = new Sys_SessionLogDao();
            IsOperationSuccessful = lSys_SessionLogDao.Insert(lSys_SessionLogEntity);


            return IsOperationSuccessful;
        }

        public List<Sys_SessionLog> GetAllSys_SessionLogDetails()
        {
            List<Sys_SessionLog> lSys_SessionLogList = new List<Sys_SessionLog>();
            Sys_SessionLogDao lSys_SessionLogDao = new Sys_SessionLogDao();
            lSys_SessionLogList = lSys_SessionLogDao.GetAll();

            return lSys_SessionLogList;
        }
        #endregion

        #region ProjectMemberMaster
        public bool SaveProjectMemberDetails(ProjectMemberMaster lProjectMemberMasterEntity)
        {
            bool IsOperationSuccessful = false;


            ProjectMemberMasterDao lProjectMemberMasterDao = new ProjectMemberMasterDao();
            IsOperationSuccessful = lProjectMemberMasterDao.Insert(lProjectMemberMasterEntity);


            return IsOperationSuccessful;
        }

        public bool UpdateProjectMemberDetails(ProjectMemberMaster lProjectMemberMasterEntity)
        {
            bool IsOperationSuccessful = false;


            ProjectMemberMasterDao lProjectMemberMasterDao = new ProjectMemberMasterDao();
            IsOperationSuccessful = lProjectMemberMasterDao.Update(lProjectMemberMasterEntity);


            return IsOperationSuccessful;
        }

        public ProjectMemberMaster GetProjectMemberDetailsByKey(int ProjectMemberKey)
        {
            ProjectMemberMasterDao lProjectMemberMasterDao = new ProjectMemberMasterDao();
            ProjectMemberMaster lProjectMemberMaster = lProjectMemberMasterDao.GetByProjectMemberKey(ProjectMemberKey);

            if (lProjectMemberMaster == null)
            {
                return null;
            }
            else
            {
                return lProjectMemberMaster;
            }
        }

        public List<ProjectMemberMaster> GetAllProjectMemberDetails()
        {
            List<ProjectMemberMaster> lProjectMemberMaster = new List<ProjectMemberMaster>();
            ProjectMemberMasterDao lProjectMemberMasterDao = new ProjectMemberMasterDao();
            lProjectMemberMaster = lProjectMemberMasterDao.GetAll();

            return lProjectMemberMaster;
        }
        #endregion
  

        #endregion
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods

        #endregion Private Methods


    }
}
